package com.px.controller;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.px.dal.ReviewProjectRelMapper;
import com.px.model.ReviewProjectRel;
import com.px.model.getData;
import com.px.unit.Response;

@Controller
public class ReviewprojectRelController {

//把当前类注解为控制器

	ReviewProjectRelMapper review_mapper = new ReviewProjectRelMapper();

	/**
	 * 获取所有评审组项目关系
	 * 
	 */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/getAllReviewProjectRel", method = RequestMethod.GET, produces = "application/json; utf-8")
	public Response getAllReviewProjectRel() {
		getData mydata = new getData();
		List<ReviewProjectRel> reviewProjectRelsList = review_mapper.getAllReviewProjectRel();
		mydata.setReviewProjectRelsList(reviewProjectRelsList);
		return new Response(200, "获取成功！", mydata);
	}

	/**
	 * 获取指定Id评审组项目关系
	 * 
	 */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/getReviewProjectByReviewProjectId", method = RequestMethod.POST, produces = "application/json; utf-8")
	public Response getReviewProjectByReviewProjectId(@RequestBody ReviewProjectRel review) {
		if (review != null) {
			// 判断查询的用户是否存在
			int checkid = review_mapper.checkReviewProjectRelIdIsExist(review);
			if (!(checkid > 0)) {
				return new Response(304, "该用户不存在");
			} else {
				// 示例化一个自定义对象，用来传递我们需要的参数
				getData mydata = new getData();
				List<ReviewProjectRel> reviewProjectRelsList = review_mapper.getReviewProjectByReviewProjectId(review);
				mydata.setReviewProjectRelsList(reviewProjectRelsList);
				return new Response(200, "获取成功！", mydata);
			}
		}
		return new Response(304, "获取失败！");
	}

	/**
	 * 添加评审组项目关系
	 * 
	 */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/insertReviewProjectRel", method = RequestMethod.POST, produces = "application/json; utf-8")
	public Response insertReviewProjectRel(@RequestBody ReviewProjectRel review) {

		if (review != null) {

			// 判断添加的会员单位负责人账户不能重复
			int checkname = review_mapper.checkReviewProjectRelIdIsExist(review);
			if (checkname > 0) {
				return new Response(304, "已存在同名帐号");
			} else {
				int counts = review_mapper.insertReviewProjectRel(review);
				if (counts > 0) {
					return new Response(200, "添加成功！");
				} else {
					return new Response(304, "添加失败！");
				}
			}
		}
		return new Response(304, "添加失败！");
	}

	/**
	 * 根据评审组项目关系ID删除评审组项目关系
	 * 
	 */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/deleteReviewProjectRelByReviewProjectRelid", method = RequestMethod.DELETE, produces = "application/json; utf-8")
	public Response deleteReviewProjectRelByReviewProjectRelid(@RequestBody ReviewProjectRel review) {

		if (review != null) {

			// 判断删除的用户是否存在
			int checkid = review_mapper.checkReviewProjectRelIdIsExist(review);
			if (!(checkid > 0)) {
				return new Response(304, "该用户不存在");
			} else {
				int counts = review_mapper.deleteReviewProjectRelByReviewProjectRelid(review);
				if (counts > 0) {
					return new Response(200, "删除成功！");
				} else {
					return new Response(304, "删除失败！");
				}
			}
		}
		return new Response(304, "删除失败！");
	}

	/**
	 * 根据评审组项目关系ID修改评审组项目关系
	 * 
	 */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/updateReviewProjectRelByReviewProjectRelId", method = RequestMethod.PUT, produces = "application/json; utf-8")
	public Response updateReviewProjectRelByReviewProjectRelId(@RequestBody ReviewProjectRel review) {

		if (review != null) {

			// 判断修改的用户是否存在
			int checkid = review_mapper.checkReviewProjectRelIdIsExist(review);
			if (!(checkid > 0)) {
				return new Response(304, "该用户不存在");
			} else {
				int counts = review_mapper.updateReviewProjectRelByReviewProjectRelId(review);
				if (counts > 0) {
					return new Response(200, "修改成功！");
				} else {
					return new Response(304, "修改失败！");
				}
			}
		}
		return new Response(304, "修改失败！");
	}

}
